//
//  MainViewController.swift
//  Weibo
//
//  Created by huangjunyang on 15/10/1.
//  Copyright © 2015年 hjy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

     //加载创建好的子控制器
        addChildViewControllers()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        //创建button
        setCompossButton()
        
        }
    
    
    private func setCompossButton() {
        
        //设置
        
        let w = tabBar.bounds.width/CGFloat (viewControllers!.count)
        
        let h = tabBar.bounds.height
        
       // loadButton.frame = CGRect(x: 2*w, y: 0, width: w, height: h)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        loadButton.frame = CGRectOffset(rect, 2*w, 0);
        
    }
       func clickComposedButton () {
        
        print(__FUNCTION__)
    }

    
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName:"tabbar_message_center_highlighted")
        
        addChildViewController(MessageViewController(), title: "消息", imageName:"tabbar_home")
        
        
        //为了把中间的部分隔开在这里创建了一个空的控制器
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName:"tabbar_discover_highlighted")
        addChildViewController(ProfileViewController(), title: "我", imageName:"tabbar_profile")
    }
    

    
    private func addChildViewController (vc: UIViewController,title: String,imageName:String ) {
        
        //创建子控制 把他添加到主控制器当中来
        tabBar.tintColor = UIColor.orangeColor()
        
        vc.title = title
        vc.tabBarItem.image = UIImage(named:imageName)
        
        let nav = UINavigationController()
        
        nav.addChildViewController(vc)
        
        addChildViewController(nav)
   

        
    }
    
    
    private lazy var loadButton : UIButton = {
       
        var btn = UIButton()
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState:UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState:UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
    
        self.tabBar.addSubview(btn)
          btn.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    
  
    
}
